package edu.gyc.hiwxm.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import edu.gyc.hiwxm.exception.BookingException;
import edu.gyc.hiwxm.model.Booking;
import edu.gyc.hiwxm.model.Vip;
import edu.gyc.hiwxm.model.VipBooking;
import edu.gyc.hiwxm.service.BookingService;
import edu.gyc.hiwxm.service.GirlService;
import edu.gyc.hiwxm.service.VipBookingService;
import edu.gyc.hiwxm.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-01-01
 */
@Controller
@RequestMapping("/vip")
public class VipController {
    @Autowired
    private VipService vipService;

    @Autowired
    private BookingService bookingService;

    @Autowired
    private VipBookingService vipBookingService;

    @Autowired
    private GirlService girlService;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/reg")
    public String reg() {
        return "reg";
    }
    @RequestMapping("/exit")
    public String exit(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
    @PostMapping("/dologin")
    public String dologin(Vip vip, HttpSession session) {

        Vip user = vipService.lambdaQuery().eq(Vip::getName, vip.getName()).one();
        if (user == null) {
            throw new BookingException("用户名错误");
        }

        session.setAttribute("user", user);
        return "redirect:/";
    }

    @PostMapping("/doreg")
    public String doreg(Vip vip, Model model) {

        Vip user = vipService.lambdaQuery().eq(Vip::getName, vip.getName()).one();
        if (user != null) {
            throw new BookingException("用户名已占用");
        }

        vipService.save(vip);
        model.addAttribute("msg", "恭喜" + vip.getName() + " 注册成功，土豪我们做朋友吧！");
        return "success";
    }

    @RequestMapping("/detail")
    public String detail(HttpSession session, Model model) {

        Vip user = (Vip) session.getAttribute("user");
        if (user == null) {
            throw new BookingException("用户不存在，请登录");
        }

        List<Booking> bookings = bookingService.getVipBookingReport(user.getId());
        int balance=vipService.lambdaQuery().eq(Vip::getId,user.getId()).one().getSubscriptionFee();
        model.addAttribute("moneny", balance);
        model.addAttribute("name", user.getName());
        model.addAttribute("bookings", bookings);
        return "vipdetail";
    }

    @RequestMapping("/moneydetail")
    public String monenydetail(HttpSession session, Model model) {

        Vip user = (Vip) session.getAttribute("user");
        if (user == null) {
            throw new BookingException("用户不存在，请登录");
        }

        List<VipBooking> vipBookings = vipBookingService.list(Wrappers.<VipBooking>lambdaQuery()
                .eq(VipBooking::getUserId, user.getId())
                .orderByDesc(VipBooking::getTransactionTime)
          );

        for (VipBooking vipBooking : vipBookings) {
            String girlName = girlService.getById(vipBooking.getGirlId()).getName();
            vipBooking.setGirlName(girlName);
        }
        int balance=vipService.lambdaQuery().eq(Vip::getId,user.getId()).one().getSubscriptionFee();
        model.addAttribute("moneny", balance);
        model.addAttribute("name", user.getName());
        model.addAttribute("vipbookings", vipBookings);
        return "moneydetail";
    }
}

